package com.neusoft.yuejian_6.fragment;

import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.neusoft.yuejian_6.adapter.ActivityListAdapter;
import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.Activity;
import com.neusoft.yuejian_6.model.ActivityModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeJingxuanFragment extends Fragment {

    private RecyclerView recyclerView;
    private List mDataList;
    private View view=null;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_jingxuan, container, false);

        initViews();
        initEvents();
        initData();
        return view;

    }
    private void initViews(){

        recyclerView= (RecyclerView)view.findViewById(R.id.rv_jingxuan);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
      //  new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        SpacesItemDecoration decoration=new SpacesItemDecoration(16);
        recyclerView.addItemDecoration(decoration);

    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpacesItemDecoration(int space) {
            this.space=space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }

    private void  initEvents(){

    }

    private void initData()
    {

        ActivityModel activityModel = new ActivityModel();
        Call call = activityModel.Activity();
        call.enqueue(new Callback<List<Activity>>() {
            @Override
            public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
                //mDataList= (List) response.body();
                List<Activity> data=response.body();
                System.out.printf("data");
                recyclerView.setAdapter(new ActivityListAdapter(getActivity(),data));
            }

            @Override
            public void onFailure(Call<List<Activity>> call, Throwable t) {
                Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
            }
        });


    }


}
